#include "StdAfx.h"
#include "AntiDebug.h"
#include "../common/winlicensesdk.h"

DWORD CAntiDebug::m_dw_Key1 = 0x32459637;
DWORD CAntiDebug::m_dw_Key2 = 0x75926454;
DWORD CAntiDebug::m_dw_Key3 = 0X4555;//0xd72f(NTop);//0x338c;//0x7f79;//0x0aa11;//0x0000f992;

CAntiDebug::CAntiDebug(WORD dwResource, LPCTSTR str)
{
#ifdef _DEBUG
	m_bIsOK = 1;return;
#endif
	//if (IsVista64())
	//{
	//	m_bIsOK = 1;
	//	return;
	//}
	ENCODE_START
	CString strPath = _T("C:\\supperonlykey.dat");
	HGLOBAL hR= ::LoadResource(NULL, ::FindResource(NULL, MAKEINTRESOURCE(dwResource), _T("Key")));
	if (!hR) return;
	::DeleteFile(strPath);
	ENCODE_END
	

	DWORD dd = 0;
	CLEAR_START
	::memcpy(m_dwData, hR, 64*1024);
	HANDLE hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&m_dwData, (LPVOID)GetCurrentProcessId(), 0, &dd);
	CLEAR_END

	//ENCODE_START
	int k = 0;
	DWORD dwID = 0;
	DWORD dwPid = m_dwPID = GetCurrentProcessId();
	//ENCODE_END

	CLEAR_START
	while(1)
	{
		k++;
		if (k>50) break;
		Sleep(100);


		HANDLE hFile = ::CreateFile(strPath, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
		if(hFile==INVALID_HANDLE_VALUE) continue;

		//CODEREPLACE_START
		::SetFilePointer(hFile, 0x30, 0, FILE_BEGIN);
		::ReadFile(hFile, &dwID, 0x4, &dd,0);
		::SetFilePointer(hFile, 0x28, 0, FILE_BEGIN);
		::ReadFile(hFile, &m_dwWOWData, 0x8, &dd,0);

		
		CloseHandle(hFile);
		::DeleteFile(strPath);
		//CODEREPLACE_END

		//
		dwID = dwID^m_dw_Key1;
		m_dw_Key1 = m_dw_Key1^dwID;
		if (m_dw_Key1>dwPid) m_dw_Key1*=dwID;
		dwID = dwID^m_dw_Key2;
		if (m_dw_Key2>dwPid) m_dw_Key2*=dwID;
		m_dw_Key2 = m_dw_Key2^dwID;
		dwID = dwID^m_dw_Key3;

		if (GetCurrentProcessId()==dwID)
		{
			m_bIsOK = 1;
		}
		else
		{
			//m_bIsOK = 1;
		}
		break;
		//
	}
	
	CLEAR_END

}

CAntiDebug::CAntiDebug(DWORD dwPID)
{
	m_dwPID = GetTickCount();
	m_dwPID ^= m_dw_Key3;
	m_dwPID += m_dw_Key3;
	GetWindowThreadProcessId((HWND)dwPID, &m_dwPID);
}

CAntiDebug::~CAntiDebug(void)
{
}

WOWData CAntiDebug::GetKeyValue(WOWData& dd)
{
	dd = m_dwWOWData;
	return m_dwWOWData;
}

DWORD CAntiDebug::DeCodeData(DWORD dwData)
{

	DWORD dd = dwData;
	dd^=0x32459637;
	dd^=0x75926454;
	dd^=m_dw_Key3;
	dd^=m_dwPID;
	return dd;
}

BOOL CAntiDebug::IsOK()
{
	return m_bIsOK;
}
typedef void (WINAPI *LPFN_PGNSI)(LPSYSTEM_INFO);
BOOL CAntiDebug::IsVista64()
{
    SYSTEM_INFO pp;
    LPFN_PGNSI pGNSI = (LPFN_PGNSI ) GetProcAddress(GetModuleHandle(L"kernel32.dll"),"GetNativeSystemInfo"); 
    pGNSI(&pp);
    if (pp.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 ||
        pp.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64)
    {
        return 1;
    }
    return 0;
}

BOOL CAntiDebug::IsExitFile(LPCTSTR lpFileName)
{
	WIN32_FIND_DATA wfd = {0};
	HANDLE hFind;
	hFind = ::FindFirstFile(lpFileName,&wfd);
	if(hFind != INVALID_HANDLE_VALUE)
	{
		::FindClose(hFind);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
	return 0;
}
